package com.lonely.common.exception;


import com.lonely.common.core.domain.api.IResultCode;
import com.lonely.common.enums.ResultCodeEnum;
import com.lonely.common.utils.PlaceholderUtil;

/**
 * 异常类
 *
 * @author fzzf
 */
public class BusinessException extends RuntimeException {

    private final IResultCode resultCode;

    /**
     * 业务异常是否发送报警，默认不发送
     */
    private Boolean waringMsg = false;

    public BusinessException(String message) {
        super(message, new Throwable());
        this.resultCode = ResultCodeEnum.FAILURE;
    }

    public BusinessException(IResultCode resultCode) {
        super(resultCode.getMessage());
        this.resultCode = resultCode;
    }

    public BusinessException(IResultCode resultCode, Boolean waringMsg) {
        super(resultCode.getMessage());
        this.resultCode = resultCode;
        this.waringMsg = waringMsg;
    }

    public BusinessException(IResultCode resultCode, Object... values) {
        super(PlaceholderUtil.getDefaultResolver().resolve(resultCode.getMessage(), values));
        this.resultCode = new ExceptionResultCode(resultCode.getCode(),
                PlaceholderUtil.getDefaultResolver().resolve(resultCode.getMessage(), values));
    }

    public BusinessException(IResultCode resultCode, Boolean waringMsg, Object... values) {
        super(PlaceholderUtil.getDefaultResolver().resolve(resultCode.getMessage(), values));
        this.resultCode = new ExceptionResultCode(resultCode.getCode(),
                PlaceholderUtil.getDefaultResolver().resolve(resultCode.getMessage(), values));
        this.waringMsg = waringMsg;
    }

    public BusinessException(IResultCode resultCode, Throwable cause) {
        super(cause);
        this.resultCode = resultCode;
    }

    public BusinessException(String message, Throwable cause) {
        super(message, cause);
        this.resultCode = ResultCodeEnum.FAILURE;
    }

    public IResultCode getResultCode() {
        return this.resultCode;
    }

    public Boolean getWaringMsg() {
        return waringMsg;
    }
}
